#!/bin/sh
exec @GUILE@ -s $0 "$@"
!#

;; Spit out the environment variable settings needed based on
;; arguments listing gnc-module-dirs, guile-load-dirs, and
;; library-dirs

(use-modules (srfi srfi-13) (srfi srfi-14)) ;; for string-tokenize
(debug-enable 'backtrace)
(debug-enable 'debug)
(read-enable 'positions)

;; Are we on MS Windows here? If yes, make this a #t.
;; (utsname:sysname (uname)) wasn't available in my guile-1.6.7 on
;; mingw, so I don't know an automated way to do this so far.
(define is-windows?
  (let ((ostype (getenv "OSTYPE")))
    (and ostype
         (string=? ostype "msys"))))

(define args (cdr (command-line)))
(define display-exports? #t)

(define gnc-module-dirs '())
(define guile-load-dirs '())
(define library-dirs '())

(define (usage-death)
  (display "Usage: gnc-test-env [ --no-exports ]\n")
  (display "                    [ (--gnc-module-dir dir | \n")
  (display "                       --guile-load-dir dir | \n")
  (display "                       --library-dir dir) ... ]\n")
  (exit 1))

(define (process-args! args)
  (let loop ((rest args))
    (cond
     ((null? rest) #t)
     ((string=? "--gnc-module-dir" (car rest))
      (set! gnc-module-dirs (cons (cadr rest) gnc-module-dirs))
      (loop (cddr rest)))
     ((string=? "--guile-load-dir" (car rest))
      (set! guile-load-dirs (cons (cadr rest) guile-load-dirs))
      (loop (cddr rest)))
     ((string=? "--library-dir" (car rest))
      (set! library-dirs (cons (cadr rest) library-dirs))
      (loop (cddr rest)))
     (else (usage-death))))
  (set! gnc-module-dirs (reverse gnc-module-dirs))
  (set! guile-load-dirs (reverse guile-load-dirs)))

;; The character set of everything except a directory separator as
;; necessary for string-tokenize below
(define char-set-path
  (char-set-adjoin
   (char-set-delete char-set:graphic #\/)
   #\ ))

;; The directory separator string.
(define dir-separator-string
  (if is-windows?
      "\\\\" ;; Needs to be quoted twice because of additional shell quoting
      "/"))

;; The path separator string; only needed for GUILE_LOAD_PATH
(define path-sep-str
  (if is-windows?
      ";"
      ":"))

;; Adapt the directory separator character in the given PATH and
;; return the result.
(define (adapt-dirsep path)
  (string-join
   (string-tokenize path char-set-path)
   dir-separator-string))

(if (and (not (null? args))
         (string=? "--no-exports" (car args)))
    (begin
      (set! display-exports? #f)
      (set! args (cdr args))))

(if (null? args) (exit 0))

(process-args! args)

(define (get-dir-adder env-name dir-list item-suffix separator)
  (string-append env-name "=\""
                 (apply string-append
                        (map
                         (lambda (dir)
                           (string-append dir item-suffix separator))
                         dir-list))
                 "${" env-name "}\" "))

(display
 (adapt-dirsep
  (get-dir-adder "GNC_MODULE_PATH" gnc-module-dirs "/.libs" path-sep-str)))

(display
 (adapt-dirsep
  (get-dir-adder "GUILE_LOAD_PATH" guile-load-dirs "" path-sep-str)))

(display
 (adapt-dirsep
  (get-dir-adder "LD_LIBRARY_PATH" library-dirs "/.libs" path-sep-str)))

(display
 (adapt-dirsep
  (get-dir-adder "DYLD_LIBRARY_PATH" library-dirs "/.libs" path-sep-str)))

(if is-windows?
    (display
     (get-dir-adder "PATH" library-dirs "/.libs" ":")))

(if display-exports?
    (begin
      (display "; ")
      (display " export GNC_MODULE_PATH;")
      (display " export GUILE_LOAD_PATH;")
      (display " export LD_LIBRARY_PATH;")
      (display " export DYLD_LIBRARY_PATH;")
      (if is-windows?
          (display " export PATH;"))))

;; Local Variables:
;; mode: scheme
;; End:
